// memozie 翻译为 记忆化

function memoize(func) {
  const cache = new Map();

  const values = function (obj) {
    if (typeof func !== "function") {
      throw new Error("func 类型错误");
    }
    const result = func(obj);
    if (!cache.has(obj)) {
      cache.set(obj, result);
    }
    return cache.get(obj);
  };
  Object.assign(values, {
    cache,
  });
  return values;
}

var object = { a: 1, b: 2 };
var other = { c: 3, d: 4 };

var values = memoize((obj) => Object.values(obj));

console.log(values(object)); // [1,2]

console.log(values(other)); // [3,4]

object.a = 2;
console.log(values(object)); // [1,2]

values.cache.set(object, ["a,b"]);

console.log(values(object)); // ["a,b"]
